package com.yunche.postloan.utils;


import java.lang.reflect.Field;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;
import java.util.HashMap;
import java.util.Map;

/**
 * @Desc TODO
 * ----------------
 * @CLASS_NAME: UpdateAnnotationValueUtil
 * ----------------
 * @Author jovi
 * ----------------
 * @Date 2019-12-04 14:47
 */
public class UpdateAnnotationValueUtil {

    public static void updateValue(Class clazz, Map<String, String> filesMap) throws NoSuchFieldException, IllegalAccessException {

        Class<?> cls = clazz;
        //获取所有字段
        Field[] declaredFields = cls.getDeclaredFields();


        for (Field declaredField : declaredFields) {
            for (Map.Entry<String, String> stringStringEntry : filesMap.entrySet()) {
                if (declaredField.getName().equals(stringStringEntry.getKey())) {
                    //获取注解
                    com.yunche.postloan.utils.ExcelVOAttribute annotation = declaredField.getAnnotation(com.yunche.postloan.utils.ExcelVOAttribute.class);
                    System.out.println("修改前.....");
                    System.out.println("名称：" + annotation.name() + "\t 列名：" + annotation.column());

                    //代理获取
                    InvocationHandler invocationHandler = Proxy.getInvocationHandler(annotation);
                    Field value = invocationHandler.getClass().getDeclaredField("memberValues");
                    value.setAccessible(true);
                    Map<String, Object> memberValues = (Map<String, Object>) value.get(invocationHandler);
                    memberValues.put("name", stringStringEntry.getValue());
                    System.out.println("修改后.....");
                    System.out.println("名称：" + annotation.name() + "\t 列名：" + annotation.column());

                    System.out.println(declaredField.getAnnotation(com.yunche.postloan.utils.ExcelVOAttribute.class).name());
                    System.out.println(declaredField.getName());
                }
            }

        }


    }

    public static void main(String[] args) throws IllegalAccessException, NoSuchFieldException {

//        Map<String, String> filesMap = new HashMap<>();
//        filesMap.put("userId", "idnew");
//        filesMap.put("userName", "姓名new");
//        filesMap.put("sex", "性别new");
//        updateValue(User.class, filesMap);
    }


}
